<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <page-filter
      v-model:query="dialogInfoFilterInfo.query"
      :filter-list="dialogInfoFilterInfo.list"
    >
    </page-filter>
    <div class="flex_full_item">
      <!-- 表格 -->
      <page-table
        :refresh="dialogTableInfo.refresh"
        :init-curpage="dialogTableInfo.initCurpage"
        v-model:data="dialogTableInfo.data"
        :api="dialogTableInfo.api"
        :query="dialogInfoFilterInfo.query"
        :field-list="dialogTableInfo.fieldList"
        :isDynamic="true"
        @handleEvent="handleEvent"
      ></page-table>
    </div>
  </page-dialog>
</template>

<script setup name="selectOrder">
import {
  getOtherStoreOrdersBillRelatedPage,
  getBillRelatedPage,
  withTableReceivingOrderBillRelatedPage,
} from '@/api/stock'
import PageTable from '@/components/DialogTable/index'
import PageDialog from '@/components/PageDialog/index'
import PageFilter from '@/components/PageFilter/index'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const props = defineProps({
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  otherQueryList: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

const dialogInfoFilterInfo = reactive({
  query: {},
  list: [
    {
      type: 'slot',
      value: 'select',
    },
    {
      type: 'input',
      label: '单号',
      value: 'orderNoLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
  options: [
    { value: 'receiving_order', label: '收货单' },
    { value: 'other_store', label: '其他入库单' },
    { value: 'with_table_receiving_order', label: '手术跟台收货' },
  ],
})
const typeList = reactive({
  receiving_order: getBillRelatedPage,
  other_store: getOtherStoreOrdersBillRelatedPage,
  with_table_receiving_order: withTableReceivingOrderBillRelatedPage,
})
const optionsValue = ref('receiving_order')
const dialogInfo = reactive({
  width: '60%',
  visible: false,
  title: '选择收货单',
  btLoading: false,
  btList: [
    { label: '关闭', event: closeDialog, show: true },
    {
      label: '保存',
      type: 'primary',
      event: selectOrder,
      saveLoading: false,
      show: true,
    },
  ],
})
const dialogTableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: null,
  selections: [],
  fieldList: [
    { label: '收货单号', value: 'orderNo' },
    { label: '入库时间', value: 'storeTime' },
    {
      label: '金额',
      value: 'billAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
    },
  ],
})

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})
onMounted(() => {
  proxy.$nextTick(() => {
    let otherQueryList = props.otherQueryList[optionsValue.value]
    dialogInfoFilterInfo.query = {
      ...props.query,
      idNotIn: otherQueryList.idNotIn.toString(),
      idMustIn: otherQueryList.idMustIn.toString(),
      sort: otherQueryList.sort,
    }
    getList()
  })
})

function getList() {
  dialogTableInfo.api = getBillRelatedPage
  dialogTableInfo.refresh = Math.random()
}
function handleChange(val) {
  let otherQueryList = props.otherQueryList[val]
  dialogInfoFilterInfo.query = {
    ...props.query,
    idNotIn: otherQueryList.idNotIn.toString(),
    idMustIn: otherQueryList.idMustIn.toString(),
    sort: otherQueryList.sort,
  }
  dialogTableInfo.api = typeList[val]
  search()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      dialogTableInfo.selections = [...data]
      dialogTableInfo.selections = dialogTableInfo.selections.map((item) => {
        item.receivingId = item.id
        return item
      })
      break
  }
}
//选择产品
function selectOrder(rows) {
  if (rows) {
    dialogTableInfo.selections = dialogTableInfo.selections.map((item) => {
      return {
        ...item,
        receivingType: optionsValue.value,
      }
    })
    emit('callback', dialogTableInfo.selections)
    closeDialog()
  }
}
function closeDialog() {
  shows.value = false
  emit('reset')
}
function search() {
  // 重置分页
  dialogTableInfo.initCurpage = Math.random()
  dialogTableInfo.refresh = Math.random()
}
</script>

<style scoped></style>
